﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Telerik.Web.UI;
using SMA.GENTEv1;

public partial class Protegidos_Administracao_Cadastros__popup_acpIncluirContratoGestor : PageBase
{
    protected int idGestor = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["IdGestor"] != null)
            idGestor = Int32.Parse(Request.QueryString["IdGestor"].ToString());

        Pesquisa();
    }

    #region Eventos
    protected void btnFechar_Click(object sender, EventArgs e)
    {
        ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "CancelEdit();", true);
    }
    protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        Pesquisa();
    }
    protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {

            int idContrato = Int32.Parse(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["IdContrato"].ToString());

            ContratoGestor contratoGestor = new ContratoGestor();
            contratoGestor.Contrato = new Contrato(idContrato);
            contratoGestor.Pessoa = new Pessoa(idGestor);
            contratoGestor.Status = new Status(1);

            contratoGestor.Salvar();

            ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "CloseAndRebind('IsertedGestor');", true);
            //Server.Transfer("acAltContratoVigencia.aspx?idContrato=" + idContrato.ToString());
        }
    }
    #endregion

    #region Metodos
    private void Pesquisa()
    {
        RadGrid1.DataSource = CarregaContrato();
    }
    public List<Contrato> CarregaContrato()
    {
        OrganizacaoPatrocinadorPrestador organPatPrest = new OrganizacaoPatrocinadorPrestador();
        int idOrnaRelacao = organPatPrest.RetornaIdRelacao(IdPatrocinador, IdPrestador);

        Contrato contrato = new Contrato();
        List<Contrato> listContrato = contrato.SelectContratoVigencia(idOrnaRelacao);

        ContratoGestor contratoGestor = new ContratoGestor();
        List<ContratoGestor> listContratoGestor = contratoGestor.SelectPorGestor(idGestor);

        foreach (ContratoGestor item1 in listContratoGestor)
        {
            foreach (Contrato item2 in listContrato)
            {
                if (item2.IdContrato == item1.Contrato.IdContrato)
                    listContrato.Remove(item2);
                break;
            }
        }

        return listContrato;
    }
    #endregion
}
